﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DBBase;

namespace Business
{
    public partial class setting : DevExpress.XtraEditors.XtraForm
    {
        public string name;
        public setting(string sname)
        {
            name = sname;
            InitializeComponent();
        }


        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void setting_Load(object sender, EventArgs e)
        {
            try
            {
                INIfile iniex = new INIfile();
                iniex.path = Application.StartupPath + @"\Business.ini";
                text_ip.Text = DecodeBase64("utf-8", iniex.IniReadValue("设置", "数据库IP"));
                text_sqlname.Text = DecodeBase64("utf-8", iniex.IniReadValue("设置", "数据库名称"));
                text_name.Text = DecodeBase64("utf-8", iniex.IniReadValue("设置", "数据库用户名"));
                text_password.Text = DecodeBase64("utf-8", iniex.IniReadValue("设置", "数据库密码"));
                com_smqck.Text = iniex.IniReadValue("设置", "扫码枪串口");
                chbxsdj.Checked = iniex.IniReadValue("设置", "联网查询显示定检时间") == "是";
                text_ipyy.Text = iniex.IniReadValue("设置", "公众号数据库IP");
                text_sqlnameyy.Text = iniex.IniReadValue("设置", "公众号数据库名称");
                text_nameyy.Text =  iniex.IniReadValue("设置", "公众号数据库用户名");
                text_passwordyy.Text = iniex.IniReadValue("设置", "公众号数据库密码");
                if (name.Contains("admin"))
                {
                    groupBox2.Visible = true;
                }
                else
                {
                    groupBox2.Visible = false;
                }
                //txtlwwj.Text = iniex.IniReadValue("设置", "广东摩托车联网文件路径");
            }
            catch
            { }
        }

        private void but_config_Click(object sender, EventArgs e)
        {

            INIfile iniex = new INIfile();
            iniex.path = Application.StartupPath + @"\Business.ini";
            iniex.IniWriteValue("设置", "数据库IP", EncodeBase64("utf-8",text_ip.Text));
            iniex.IniWriteValue("设置", "数据库名称",  EncodeBase64("utf-8",text_sqlname.Text));
            iniex.IniWriteValue("设置", "数据库用户名",  EncodeBase64("utf-8",text_name.Text));
            iniex.IniWriteValue("设置", "数据库密码",  EncodeBase64("utf-8",text_password.Text));
            iniex.IniWriteValue("设置", "扫码枪串口", com_smqck.Text);
            iniex.IniWriteValue("设置", "联网查询显示定检时间", chbxsdj.Checked?"是":"否");

            iniex.IniWriteValue("设置", "公众号数据库IP", text_ipyy.Text);
            iniex.IniWriteValue("设置", "公众号数据库名称", text_sqlnameyy.Text);
            iniex.IniWriteValue("设置", "公众号数据库用户名", text_nameyy.Text);
            iniex.IniWriteValue("设置", "公众号数据库密码", text_passwordyy.Text);

            //iniex.IniWriteValue("设置", "广东摩托车联网文件路径", txtlwwj.Text);
            MessageBox.Show("设置完成");
        }
        ///编码
        public static string EncodeBase64(string code_type, string code)
        {
            string encode = "";
            byte[] bytes = Encoding.GetEncoding(code_type).GetBytes(code);
            try
            {
                encode = Convert.ToBase64String(bytes);
            }
            catch
            {
                encode = code;
            }
            return encode;
        }
        ///解码
        public static string DecodeBase64(string code_type, string code)
        {
            string decode = "";
            byte[] bytes = Convert.FromBase64String(code);
            try
            {
                decode = Encoding.GetEncoding(code_type).GetString(bytes);
            }
            catch
            {
                decode = code;
            }
            return decode;
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
        }

    }
}